package com.srd.phone.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.bean.BarrioInfo;
import com.srd.phone.bean.UserList;
import com.srd.phone.dialog.BarrioThreeDialog;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.UserUtils;
import com.srd.phone.utils.wheel.widget.SingleDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户管理
 */
public class UserManageActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_username, tv_id_card, tv_age, tv_sex;
    private UtilPresenter utilPresenter = null;

    private Map<String, String> map = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_manage);

        initView();
        initData();
        saveData();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void initView() {
        tv_common_title.setText("用户管理");
        findViewById(R.id.rl_change_head).setOnClickListener(this);
        findViewById(R.id.rl_change_marriage).setOnClickListener(this);
        findViewById(R.id.rl_change_house).setOnClickListener(this);
        findViewById(R.id.rl_change_car).setOnClickListener(this);
        findViewById(R.id.rl_change_account).setOnClickListener(this);

        tv_username = (TextView)findViewById(R.id.tv_username);
        tv_id_card = (TextView)findViewById(R.id.tv_id_card);
        tv_age = (TextView)findViewById(R.id.tv_age);
        tv_sex = (TextView)findViewById(R.id.tv_sex);
    }

    public void initData() {
        presenter = new UtilPresenter(requestQueue, this);
        utilPresenter = (UtilPresenter)presenter;
        setUrlArrs(Constants.USER_LIST, Constants.UPDATE_USER);
        getData();
    }

    private void getData(){
        utilPresenter.noParams(Constants.USER_LIST);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        if(Constants.USER_LIST.equals(tag)){
            UserList item = UserList.parseData(data);

            tv_username.setText(item.real);
            tv_id_card.setText(item.idCard);
            tv_age.setText(item.age + "");
            tv_sex.setText(item.sex);
        }else if(Constants.UPDATE_USER.equals(tag)){
            try {
                JSONObject jsonObject = new JSONObject(data);
                toastShow(jsonObject.optString("msg"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.rl_change_head:
                startActivity(new Intent(this, ChangeHeadActivity.class));
                break;
            case R.id.rl_change_marriage:  // 婚姻状况

                SingleDialog.makeDialog(this, Constants.MARRYDATASOURCE, new SingleDialog.OnAddressCListener() {
                    @Override
                    public void onClick(String itemName) {
                        Map<String, String> map = new HashMap<>();
                        map.put("uid", UserUtils.getUserInfo().uid);
                        map.put("marriage", map.get(itemName));
                        utilPresenter.mapParams(Constants.UPDATE_USER, map);
                    }
                }).show();

                break;
            case R.id.rl_change_house:  // 购房

                SingleDialog.makeDialog(this, Constants.HOUSEDATASOURCE, new SingleDialog.OnAddressCListener() {
                    @Override
                    public void onClick(String itemName) {

                        Map<String, String> map = new HashMap<>();
                        map.put("uid", UserUtils.getUserInfo().uid);
                        map.put("purhouse", map.get(itemName));
                        utilPresenter.mapParams(Constants.UPDATE_USER, map);
                    }
                }).show();

                break;
            case R.id.rl_change_car:  //购车

                SingleDialog.makeDialog(this, Constants.CARDATASOURCE, new SingleDialog.OnAddressCListener() {
                    @Override
                    public void onClick(String itemName) {

                        Map<String, String> map = new HashMap<>();
                        map.put("uid", UserUtils.getUserInfo().uid);
                        map.put("purcare", map.get(itemName));
                        utilPresenter.mapParams(Constants.UPDATE_USER, map);
                    }
                }).show();

                break;
            case R.id.rl_change_account:  // 户口

                BarrioThreeDialog.makeThreeDialog(this, new BarrioThreeDialog.OnAddressCListener() {
                    @Override
                    public void onClick(BarrioInfo province, BarrioInfo city, BarrioInfo district) {

                        Map<String, String> map = new HashMap<>();
                        map.put("uid", UserUtils.getUserInfo().uid);
                        map.put("domicile", province.id + "_" + city.id + "_" + district.id);
                        utilPresenter.mapParams(Constants.UPDATE_USER, map);
                    }
                }).show();
                break;
        }
    }

    private void saveData(){
        map.put("已婚", "1010");
        map.put("未婚", "1011");
        map.put("有房", "7");
        map.put("无房", "1002");
        map.put("有车", "5");
        map.put("无车", "6");
    }

}